﻿using Clipboard.Model;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.Identity.Client.Platforms.Features.DesktopOs.Kerberos;
using System.Collections.ObjectModel;
using System.Configuration;
using Clipboard.Tools;
using Clipboard.Services;
using Microsoft.Win32;

namespace Clipboard.ViewModel
{
    public partial class LoginViewModel : ObservableObject
    {
        private readonly SqlSugarHelper _db;
       

        [ObservableProperty]
        private AuthAccount authAccount = new AuthAccount();
        RegistryHelper registry = new RegistryHelper();


        public LoginViewModel(SqlSugarHelper db)
        {
            _db = db;

            _ = LoadAsync();
            //如果没有配置文件则写人 Redis key
        }

        public async Task LoadAsync()
        {
            //操作App.config 配置文件里的缓存 读取
            string email = (registry.GetValue("Email") as string)??"";
            string password = (registry.GetValue("Password") as string) ?? "";
            string oAuthId = (registry.GetValue("OAuthId") as string) ?? "";

            if (string.IsNullOrEmpty(email)&&string.IsNullOrEmpty(password)&&string.IsNullOrEmpty(oAuthId))
            {
                authAccount.Name = email;
                authAccount.Email = email;
                return;
            }

            await LoginAsync(email, password);

            OnPropertyChanged(nameof(AuthAccount));  // 手动触发属性变化通知
          
            
        }



        public async Task LoginAsync(string email,string password)
        {
            authAccount = await _db.Db.Queryable<AuthAccount>()
                .Where(record => record.Email == email)
                .Where(dataType => dataType.Password == password)
                .FirstAsync();

        

            var ip = new IpHelper().GetLocalIP();

            var authId = Guid.NewGuid().ToString();

            if (authAccount == null || authAccount?.Id <= 0)
            {
                //添加
                await _db.Db.Insertable(new AuthAccount()
                {
                    Email = email,
                    Name = email,
                    Password = password,
                    OAuthId = authId,
                    Type = "Email",
                    UpdatedTime = DateTime.Now,
                    CreatedTime = DateTime.Now,
                    Ip = ip,
                    LastLoginAddress = new IpHelper().GetGeolocation(ip).ToString() ?? ""

                }).ExecuteCommandAsync();
            }
            else
            {
                //登录
                await _db.Db.Updateable<AuthAccount>()
                    .SetColumns(it => new AuthAccount() { UpdatedTime = DateTime.Now })
                    .SetColumns(it => new AuthAccount() { Ip = ip })
                    .SetColumns(it => new AuthAccount() { LastLoginAddress = new IpHelper().GetGeolocation(ip).ToString() ?? "" })
                    .Where(it => it.Id == authAccount.Id)
                    .ExecuteCommandAsync();

                authId= authAccount.OAuthId;
            }

            registry.SetValue("Email", email);
            registry.SetValue("Password", password);
            registry.SetValue("OAuthId", authId);

        }
    }
}
